#include "ADC.h"

void ADC_Init()
{
		ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);

    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

    ROM_GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3);

    //
    // Enable sample sequence 3 with a processor signal trigger.  Sequence 3
    // will do a single sample when the processor sends a signal to start the
    // conversion.  Each ADC module has 4 programmable sequences, sequence 0
    // to sequence 3.  This example is arbitrarily using sequence 3.
    //
    ROM_ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);

    //
    // Configure step 0 on sequence 3.  Sample channel 0 (ADC_CTL_CH0) in
    // single-ended mode (default) and configure the interrupt flag
    // (ADC_CTL_IE) to be set when the sample is done.  Tell the ADC logic
    // that this is the last conversion on sequence 3 (ADC_CTL_END).  Sequence
    // 3 has only one programmable step.  Sequence 1 and 2 have 4 steps, and
    // sequence 0 has 8 programmable steps.  Since we are only doing a single
    // conversion using sequence 3 we will only configure step 0.  For more
    // information on the ADC sequences and steps, reference the datasheet.
    //
    ROM_ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE |
                             ADC_CTL_END);

    //
    // Since sample sequence 3 is now configured, it must be enabled.
    //
    ROM_ADCSequenceEnable(ADC0_BASE, 3);

    //
    // Clear the interrupt status flag.  This is done to make sure the
    // interrupt flag is cleared before we sample.
    //
    ROM_ADCIntClear(ADC0_BASE, 3);

}

void ADC_Update()
{
		uint32_t pui32ADC0Value[1];
	
    ROM_ADCProcessorTrigger(ADC0_BASE, 3);

        //
        // Wait for conversion to be completed.
        //
     while(!ROM_ADCIntStatus(ADC0_BASE, 3, false))
     {
     }

        //
        // Clear the ADC interrupt flag.
        //
     ROM_ADCIntClear(ADC0_BASE, 3);

        //
        // Read ADC Value.
        //
     ROM_ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);
		 
//		 printf("AIN0 = %4d\r\n", pui32ADC0Value[0]);
}
